package com.cooqe.basefinder;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cooqe.collection_snmp.utils.CommonUtil;
import com.cooqe.entity.ItMetric;
import com.cooqe.mapper.ItFinderMapper;
import com.cooqe.mapper.ItMetricMapper;
import com.cooqe.mapper.ItResFindMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2023/11/19-2023
 * @Description: helper
 */
@Component
@SuppressWarnings("all")
public class BaseHelper {

    @Autowired
    private ItFinderMapper itFinderMapper;
    @Autowired
    private ItResFindMapper itResFindMapper;
    @Autowired
    private ItMetricMapper itMetricMapper;

    public JSONObject getTemplateParam(String params) {
        JSONObject templateParam = JSONObject.parseObject(params);
        return templateParam;
    }

    public List<ItMetric> getMetricList(JSONObject templateParam, Class clazz) {
        //获取采集指标
        QueryWrapper<ItMetric> queryWrapper = new QueryWrapper<>();
        //采集频率： 30s、1min、5min、10min、30min、1h ...
        String frequency = templateParam.getString("frequency");
        if (StringUtils.isNotBlank(frequency)) {
            queryWrapper.eq("collect_period", frequency);
        }
        //资源分类： 交换机 路由器 防火墙 linux服务器...
        String categoryId = templateParam.getString("categoryId");
        if (StringUtils.isNotBlank(categoryId)) {
            queryWrapper.eq("category_id", categoryId);
        }
        //特殊标识符
        String metricIdentify = templateParam.getString("metricIdentify");
        if (StringUtils.isNotBlank(metricIdentify)) {
            queryWrapper.eq("metric_identify", metricIdentify);
        }
        //资源发现器 snmpFinderJob、sshFinderJob、pingFinderJob
        queryWrapper.eq("finder_id", CommonUtil.getContainerSimpleName(clazz));
        List<ItMetric> metricList = itMetricMapper.selectList(queryWrapper);

        return metricList;
    }
}